﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using QzEbook.AccessObject;
using QzEbook.Common;
using System.Text;
using System.IO;
using System.Collections.Specialized;

public partial class admin_module_favoriteBookList : System.Web.UI.UserControl
{
    private StringBuilder sb;
    private int pageSize = 9;
    private int countPage = 1;
    private int currentIndex = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
        PaginationHTML();
    }

    private void BindData()
    {
        sb = new StringBuilder();

        Favorite favoriteAction = new Favorite();
        favoriteAction.UserId = SessionManager.LoginUser.Uid;
        DataTable table=favoriteAction.selectUser();
        if (table.Rows.Count == 0)
        {
            sb.Append("<div style='padding:10px 15px; line-height:20px;color:#990000;'><h4><strong>提示:</strong></h4>你暂无收藏</div>");
            return;
        }

        DataRowCollection rows = table.Rows;
        int length = rows.Count;
        countPage = length % pageSize == 0 ? (int)(length / pageSize) : (int)(length / pageSize) + 1;
        currentIndex = Request["fi"] == null ? 1 : int.Parse(Request["fi"]);

        
        int start = (currentIndex - 1) * pageSize;
        int end = start + pageSize;
        end = end >= length ? length : end;

        Book book = new Book();
        sb.Append("<h3>我的收藏</h3>");
        sb.AppendLine("<ul class='booklist' id='booklist'>");
        for (int i = start; i < end; i++)
        {
            DataRow row = rows[i];
            book = book.bindThis(row);

            sb.AppendLine("<li>");
            sb.AppendFormat("<a href='../detail.xhtml?Bid={0}' title={0}>", book.BookId, book.Title);

            string thumb = string.Format("../{0}", book.ThumbFullPath);
            if (!File.Exists(Server.MapPath(thumb)))
            {
                thumb = "../images/siteIcon/error.gif";
            }
            sb.AppendFormat("<img src='{0}?rnd={2}' alt='查看{1}' />", thumb, book.Title, DateTime.Now.Second);
            sb.AppendLine("</a>");

            sb.AppendFormat("<ins>{0}", book.Title);

            sb.AppendLine("<cite>");

            sb.AppendFormat("<a href='../Viewer.xhtml?Bid={0}'><img src='../images/icon_swf.gif' alt='预览效果' /></a>", book.BookId);
            sb.AppendFormat("<a href='../user.xhtml?Uid={0}'><img src='../images/icon_user.gif' alt='查看作者' /></a>", book.EditorId);
            if (book.isSale)
            {
                sb.Append("<dfn><img src='../images/siteIcon/buy.gif' alt='收费' /></dfn>");
            }
            if (book.isSelf)
            {
                sb.Append("<dfn><img src='../images/siteIcon/key.gif' alt='私有' /></dfn>");
            }
            sb.Append(book.getAuditingHTML("../"));

            sb.AppendLine("</cite>");
            sb.AppendLine("</ins>");
            sb.AppendLine("</li>");
        }
        sb.AppendLine("</ul>");
    }


    private void PaginationHTML()
    {
        if (this.countPage <= 1) return;
        NameValueCollection list = Request.QueryString;
        string url = "?";
        /*string namevalue = string.Empty;
        foreach (string item in list.AllKeys)
        {
            namevalue = list[item];
            if (item.ToLower() != "fi")
            {
                url = string.Format("{0}{1}={2}&", url, item, namevalue);
            }
        }*/

        sb.Append("<ul class=\"pagination\">");

        if (currentIndex > 1)
        {
            sb.AppendFormat("<li><a href=\"{0}fi={1}\" titile=\"上一页\">«  </a></li>", url, currentIndex - 1);
        }
        for (int i = 1; i <= countPage; i++)
        {
            if (i == currentIndex)
            {
                sb.AppendFormat("<li class=\"current\">{0}</li> ", i);
            }
            else
            {
                sb.AppendFormat("<li><a href=\"{0}fi={1}\" title=第{0}页>{1}</a></li> ", url, i);
            }
        }
        if (currentIndex < countPage)
        {
            sb.AppendFormat("<li><a href=\"{0}fi={1}\" title=\"下一页\">  »</a></li>", url, currentIndex + 1);
        }
        sb.AppendFormat("<li>{0}/{1}</li> ", currentIndex, countPage);
        sb.AppendLine("</ul>");
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (sb != null)
        {
            writer.Write(sb.ToString());
        }
        base.Render(writer);
    }
}
